package com.cy.module;

import com.cy.BasicController;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.BrewedLog;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;
import java.util.Map;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * 冲泡记录统计
 */
@ControllerBind(controllerKey = "/clmp/sumBrewed")
public class SumBrewedController extends BasicController {

    public void index(){
        String startTime = this.getPara("startTime");
        String endTime = this.getPara("endTime");
        String timeType = this.getPara("timeType");
        String deviceIds = this.getPara("deviceIds");
        Map<String, Object> result = null;

        String[] ids = null;
        if (isNotBlank(deviceIds)){
            ids = deviceIds.split(",");
        }
        switch (timeType) {
            case "year":
                result = BrewedLog.dao.sumByYear(startTime, endTime, ids);
                break;
            case "quarter":
                result = BrewedLog.dao.sumByQuarter(startTime, endTime, ids);
                break;
            case "month":
                result = BrewedLog.dao.sumByMonth(startTime, endTime, ids);
                break;
            case "week":
                result = BrewedLog.dao.sumByWeek(startTime, endTime, ids);
                break;
            case "day":
                result = BrewedLog.dao.sumByDay(startTime, endTime, ids);
                break;
            case "hour":
                result = BrewedLog.dao.sumByHour(startTime, endTime, ids);
                break;
        }
        result.put("sumAll", Db.queryLong("SELECT COUNT(1) FROM brewed_log_archived WHERE brew_type = 1 OR brew_type = 2"));//总冲泡数
        renderJson(result);
    }

    /*
    地域分布统计
     */
    public void sumByMap(){
        String startTime = this.getPara("startTime");
        String endTime = this.getPara("endTime");
        String timeType = this.getPara("timeType");
        String deviceIds = this.getPara("deviceIds");
        String[] ids = null;
        if (isNotBlank(deviceIds)){
            ids = deviceIds.split(",");
        }
        List<Record> result = null;
        switch (timeType) {
            case "year":
                result = BrewedLog.dao.findByYear(startTime, endTime, ids);
                break;
            case "quarter":
            case "month":
                result = BrewedLog.dao.findByMonth(startTime, endTime, ids);
                break;
            case "week":
            case "day":
            case "hour":
                result = BrewedLog.dao.findByDay(startTime, endTime, ids);
                break;
        }
        renderJson(result);
    }

}
